package com.java.second_goods.controller;

import com.java.second_goods.common.AjaxResult;
import com.java.second_goods.entity.Review;
import com.java.second_goods.entity.ReviewImage;
import com.java.second_goods.service.ReviewService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 评价信息
 */
@RestController
@RequestMapping("/api/review")
@Tag(name = "评价管理", description = "评价相关接口")
public class ReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**
     * 获取评价列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取评价列表", description = "获取评价列表")
    public AjaxResult list(Review review) {
        List<Review> list = reviewService.selectReviewList(review);
        return success(list);
    }

    /**
     * 获取评价详细信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取评价详细信息", description = "根据评价ID获取评价详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(reviewService.selectReviewById(id));
    }

    /**
     * 根据订单ID获取评价
     */
    @GetMapping("/booking/{bookingId}")
    @Operation(summary = "根据订单ID获取评价", description = "根据订单ID获取评价")
    public AjaxResult getReviewByBookingId(@PathVariable("bookingId") String bookingId) {
        return success(reviewService.selectReviewByBookingId(bookingId));
    }

    /**
     * 根据厨师ID获取评价列表
     */
    @GetMapping("/chef/{chefId}")
    @Operation(summary = "根据厨师ID获取评价列表", description = "根据厨师ID获取评价列表")
    public AjaxResult getReviewsByChefId(@PathVariable("chefId") Integer chefId) {
        List<Review> reviews = reviewService.selectReviewsByChefId(chefId);
        return success(reviews);
    }

    /**
     * 根据用户ID获取评价列表
     */
    @GetMapping("/user/{userId}")
    @Operation(summary = "根据用户ID获取评价列表", description = "根据用户ID获取评价列表")
    public AjaxResult getReviewsByUserId(@PathVariable("userId") Integer userId) {
        List<Review> reviews = reviewService.selectReviewsByUserId(userId);
        return success(reviews);
    }

    /**
     * 新增评价
     */
    @PostMapping
    @Operation(summary = "新增评价", description = "新增评价")
    public AjaxResult add(@RequestBody Map<String, Object> params) {
        try {
            Review review = new Review();
            // 设置评价信息
            review.setBookingId((String) params.get("bookingId"));
            review.setUserId((Integer) params.get("userId"));
            review.setChefId((Integer) params.get("chefId"));
            review.setRating((Integer) params.get("rating"));
            review.setContent((String) params.get("content"));
            review.setIsAnonymous((Boolean) params.get("isAnonymous"));
            review.setTags((String) params.get("tags"));

            // 获取图片列表
            @SuppressWarnings("unchecked")
            List<String> images = (List<String>) params.get("images");

            return toAjax(reviewService.insertReview(review, images));
        } catch (Exception e) {
            logger.error("新增评价失败：", e);
            return error("新增评价失败：" + e.getMessage());
        }
    }

    /**
     * 修改评价
     */
    @PutMapping
    @Operation(summary = "修改评价", description = "修改评价")
    public AjaxResult edit(@RequestBody Map<String, Object> params) {
        try {
            Integer id = (Integer) params.get("id");
            Review review = reviewService.selectReviewById(id);
            if (review == null) {
                return error("评价不存在");
            }

            // 设置评价信息
            review.setRating((Integer) params.get("rating"));
            review.setContent((String) params.get("content"));
            review.setIsAnonymous((Boolean) params.get("isAnonymous"));
            review.setTags((String) params.get("tags"));

            // 获取图片列表
            @SuppressWarnings("unchecked")
            List<String> images = (List<String>) params.get("images");

            return toAjax(reviewService.updateReview(review, images));
        } catch (Exception e) {
            logger.error("修改评价失败：", e);
            return error("修改评价失败：" + e.getMessage());
        }
    }

    /**
     * 删除评价
     */
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除评价", description = "删除评价")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(reviewService.deleteReviewByIds(ids));
    }

    /**
     * 获取评价图片
     */
    @GetMapping("/images/{reviewId}")
    @Operation(summary = "获取评价图片", description = "根据评价ID获取图片列表")
    public AjaxResult getReviewImages(@PathVariable("reviewId") Integer reviewId) {
        List<ReviewImage> images = reviewService.selectImagesByReviewId(reviewId);
        return success(images);
    }

    /**
     * 获取厨师评分
     */
    @GetMapping("/rating/{chefId}")
    @Operation(summary = "获取厨师评分", description = "获取厨师的平均评分")
    public AjaxResult getChefRating(@PathVariable("chefId") Integer chefId) {
        Double rating = reviewService.getChefAvgRating(chefId);
        return success(rating != null ? rating : 5.0); // 默认5分
    }
}